package com.guoheng.hazard.sys.notice;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guoheng.hazard.common.base.Result;
import com.guoheng.hazard.common.constant.CommonConstants;
import com.guoheng.hazard.common.enums.ErrorCodeEnum;
import com.guoheng.hazard.common.enums.NoticeTypeEnum;
import com.guoheng.hazard.common.validator.ValidEnum;
import com.guoheng.hazard.sys.mapper.entpr.NoticeMapper;
import com.guoheng.hazard.sys.model.dto.PageDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Resource
    private NoticeMapper noticeMapper;

    /**
     * 按照通知类型查询通知
     *
     * @param pageDTO 通知公告
     * @return Result
     */
    @Override
    public Result queryNoticeByType(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
        JSONObject filters = pageDTO.getFilters();
        Integer noticeType = filters.getInteger("noticeType");
        // json字符串转对象
        List<Notice> notices = noticeMapper.queryNoticeByType(noticeType);
        PageInfo<Notice> noticesPage = new PageInfo<>(notices);
        return Result.success(noticesPage);
    }

    /**
     * 根据id删除通知
     *
     * @param id 主键
     * @return Result
     */
    @Override
    public Result deleteByPrimaryKey(Integer id) {
        int i = noticeMapper.deleteByPrimaryKey(id);
        if (i > CommonConstants.DataBaseOperationStatus.IS_FAILURE) {
            return Result.success();
        }
        return Result.failure(ErrorCodeEnum.SYS_ERR_DELETE_FAILED);
    }

    @Override
    public int insert(Notice record) {
        return noticeMapper.insert(record);
    }

    /**
     * 新增通知
     *
     * @param record 通知公告
     * @return Result
     */
    @Override
    public Result insertSelective(NoticeDTO record) {
        @ValidEnum(enumClass = NoticeTypeEnum.class) Integer noticeType = record.getNoticeType();
        // 如果为4园区介绍，则只存在一条数据
        if (noticeType.equals(NoticeTypeEnum.NOTICE_TYPE_4.getCode())) {
            List<Notice> notices = noticeMapper.queryNoticeByType(noticeType);
            if (CollectionUtil.isNotEmpty(notices)) {
                return Result.failure(ErrorCodeEnum.SYS_ERR_PERSON_REPETITION_FAILED);
            }
        }
        Notice notice = new Notice();
        BeanUtils.copyProperties(record, notice);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        int i = noticeMapper.insertSelective(notice);
        if (i > CommonConstants.DataBaseOperationStatus.IS_FAILURE) {
            return Result.success();
        }
        return Result.failure(ErrorCodeEnum.SYS_ERR_CREATE_FAILED);
    }

    /**
     * 根据id查询
     *
     * @param id 主键
     * @return Result
     */
    @Override
    public Result selectByPrimaryKey(Integer id) {
        Notice notice = noticeMapper.selectByPrimaryKey(id);
        return Result.success(notice);
    }

    /**
     * 编辑通知
     *
     * @param record 通知公告
     * @return Result
     */
    @Override
    public Result updateByPrimaryKeySelective(NoticeDTO record) {
        Notice notice = new Notice();
        // 第一个拷贝到第二个
        BeanUtils.copyProperties(record, notice);
        notice.setUpdateTime(new Date());
        int i = noticeMapper.updateByPrimaryKeySelective(notice);
        if (i > CommonConstants.DataBaseOperationStatus.IS_FAILURE) {
            return Result.success();
        }
        return Result.failure(ErrorCodeEnum.SYS_ERR_UPDATE_FAILED);
    }

    @Override
    public int updateByPrimaryKey(Notice record) {
        return noticeMapper.updateByPrimaryKey(record);
    }

}
